package com.quectel.cms.controller.partybuildingactivity;


import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.partybuildingactivity.dto.PartyBuildingActivityPersonJoinBindingDto;
import com.quectel.core.module.partybuildingactivity.service.PartyBuildingActivityPersonJoinBindingService;
import com.quectel.util.common.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:38
 */
@RestController
@RequestMapping("partyBuildingActivityPersonJoinBinding")
@Tag(name = "PartyBuildingActivityPersonJoinBindingController", description = "主题活动和党员参与绑定关系相关api")
public class PartyBuildingActivityPersonJoinBindingController extends BaseController {

    @DubboReference
    private PartyBuildingActivityPersonJoinBindingService partyBuildingActivityPersonJoinBindingService;

    @GetMapping("exportSignPersonExcel")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:exportExcel")
    @Operation(summary = "导出参与人员列表")
    @Log(express = "导出参与人员列表")
    public void exportSignPersonExcel(@RequestParam Map<String, Object> params, HttpServletResponse hresp) {
        Request request = Request.configParamsNotPage(params);

        super.fillAuthorityData(params);

        List<PartyBuildingActivityPersonJoinBindingDto> result = new ArrayList<>();
        int pageNo = 0;
        params.put("limit", 1000);
        while (true) {
            params.put("offset", pageNo++ * 1000);
            List<PartyBuildingActivityPersonJoinBindingDto> list = partyBuildingActivityPersonJoinBindingService.queryList(request.getParams());
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
                break;
            }
            result.addAll(list);
            if (list.size() < 1000) {
                break;
            }
        }
        String[] headers = new String[]{"活动主题", "姓名", "联系电话","所属党组织","是否报名", "是否签到","签到时间"};
        List<String[]> dataList = new ArrayList<>();
        for (PartyBuildingActivityPersonJoinBindingDto data : result) {
            String[] item = new String[headers.length];
            item[0] = data.getSubject();
            item[1] = data.getPartyPersonName();
            item[2] = data.getMobile();
            item[3] = data.getPartyName();
            item[4] =  SysCodeUtils.easyToGetName(SysCodeKeyConstants.YES_OR_NOT, data.getJoinStatus() + "");
            item[5] =  SysCodeUtils.easyToGetName(SysCodeKeyConstants.YES_OR_NOT, data.getSignStatus() + "");
            item[6] = DateUtils.format(data.getSignTime(), DateUtils.FormatType.COMMON);
            dataList.add(item);
        }
        POIUtils.exportExcel(hresp, "主题活动签到记录", headers, dataList, "主题活动签到记录");
    }

    @GetMapping("exportJoinPersonExcel")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:exportExcel")
    @Operation(summary = "导出报名人员列表")
    @Log(express = "导出报名人员列表")
    public void exportJoinPersonExcel(@RequestParam Map<String, Object> params, HttpServletResponse hresp) {
        Request request = Request.configParamsNotPage(params);

        super.fillAuthorityData(params);

        List<PartyBuildingActivityPersonJoinBindingDto> result = new ArrayList<>();
        int pageNo = 0;
        params.put("limit", 1000);
        while (true) {
            params.put("offset", pageNo++ * 1000);
            List<PartyBuildingActivityPersonJoinBindingDto> list = partyBuildingActivityPersonJoinBindingService.queryList(request.getParams());
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
                break;
            }
            result.addAll(list);
            if (list.size() < 1000) {
                break;
            }
        }
        String[] headers = new String[]{"活动主题", "姓名", "联系电话","所属党组织","是否报名","报名时间"};
        List<String[]> dataList = new ArrayList<>();
        for (PartyBuildingActivityPersonJoinBindingDto data : result) {
            String[] item = new String[headers.length];
            item[0] = data.getSubject();
            item[1] = data.getPartyPersonName();
            item[2] = data.getMobile();
            item[3] = data.getPartyName();
            item[4] =  SysCodeUtils.easyToGetName(SysCodeKeyConstants.YES_OR_NOT, data.getJoinStatus() + "");
            item[5] = DateUtils.format(data.getJoinTime(), DateUtils.FormatType.COMMON);
            dataList.add(item);
        }
        POIUtils.exportExcel(hresp, "主题活动报名记录", headers, dataList, "主题活动报名记录");
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:list")
    @Operation(summary = "分页获取主题活动和党员参与绑定关系列表")
    public Response<Page<PartyBuildingActivityPersonJoinBindingDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = partyBuildingActivityPersonJoinBindingService.queryTotal(request.getParams());

        List<PartyBuildingActivityPersonJoinBindingDto> list = null;
        if (total > 0) {
            list = partyBuildingActivityPersonJoinBindingService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PartyBuildingActivityPersonJoinBindingDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PartyBuildingActivityPersonJoinBindingDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:edit")
    @Operation(summary = "在编辑时根据id查询主题活动和党员参与绑定关系详情")
    public Response<PartyBuildingActivityPersonJoinBindingDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyBuildingActivityPersonJoinBindingService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyBuildingActivityPersonJoinBindingDto partyBuildingActivityPersonJoinBindingDto = partyBuildingActivityPersonJoinBindingService.selectById(id);

        return Response.<PartyBuildingActivityPersonJoinBindingDto>ok().wrap(partyBuildingActivityPersonJoinBindingDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:info")
    @Operation(summary = "根据id查询主题活动和党员参与绑定关系的详情")
    public Response<PartyBuildingActivityPersonJoinBindingDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyBuildingActivityPersonJoinBindingService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyBuildingActivityPersonJoinBindingDto partyBuildingActivityPersonJoinBindingDto = partyBuildingActivityPersonJoinBindingService.selectById(id);

        return Response.<PartyBuildingActivityPersonJoinBindingDto>ok().wrap(partyBuildingActivityPersonJoinBindingDto);
    }

    /**
     * 保存
     *
     * @param partyBuildingActivityPersonJoinBindingDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:edit")
    @Operation(summary = "新增主题活动和党员参与绑定关系")
    public Response<Object> save(@RequestBody @Validated PartyBuildingActivityPersonJoinBindingDto partyBuildingActivityPersonJoinBindingDto) {

        //填充基础数据
        fillSaveCommonData(partyBuildingActivityPersonJoinBindingDto);

        partyBuildingActivityPersonJoinBindingService.save(partyBuildingActivityPersonJoinBindingDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param partyBuildingActivityPersonJoinBindingDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:edit")
    @Operation(summary = "根据id修改主题活动和党员参与绑定关系")
    public Response<Object> update(@RequestBody  @Validated PartyBuildingActivityPersonJoinBindingDto partyBuildingActivityPersonJoinBindingDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(partyBuildingActivityPersonJoinBindingDto.getId()));
        if (partyBuildingActivityPersonJoinBindingService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(partyBuildingActivityPersonJoinBindingDto);

        partyBuildingActivityPersonJoinBindingService.updateById(partyBuildingActivityPersonJoinBindingDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "partyBuildingActivityPersonJoinBinding:delete")
    @Operation(summary = "根据多条记录id批量删除主题活动和党员参与绑定关系")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (partyBuildingActivityPersonJoinBindingService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        partyBuildingActivityPersonJoinBindingService.deleteBatch(ids);

        return Response.ok();
    }
}
